<?
/*
* Smarty plugin
* -------------------------------------------------------------
* File:     function.image.php
* Type:     function
* Name:     image
* Purpose:  zwraca uri miniaturki obrazka
* -------------------------------------------------------------
*/

function smarty_function_image($param, &$smarty)
{
	
	if(!isset($param['w']) && !isset($param['h']))
	{
		return '<!-- can\'t create image - need some params  -->';
	}
	
	$_thumbDir = (isset($param['w'])?$param['w']:'0') . '-' . (isset($param['h'])?$param['h']:'0') . (isset($param['c'])? '-c' : '' ); 
	
	if(!$param['src'] || !file_exists(PATH_STATIC_CONTENT . DIRECTORY_SEPARATOR . $param['src']))
	{
		$param['src'] = 'image/Strona/default.jpg'; // zaslepka
	}
	
	$pathArr = explode('/', $param['src']);
	if(empty($pathArr)) return '<!-- can\'t create image - param src is incorrect or file doesn\'t exists  -->';
	$imgEl = array_pop($pathArr);
	$path = empty($pathArr)?'':implode('/', $pathArr);
	$_uri = $path . '/' . $_thumbDir . '/' . $imgEl;
	$newPath = PATH_STATIC_CONTENT . '/' . $_uri;
	if(file_exists($newPath)) return $_uri;
	try {
		$img = new Thumbnail(PATH_STATIC_CONTENT . DIRECTORY_SEPARATOR . $param['src']);
		$img->resize($param['w'], $param['h']);
		if(isset($param['c']))
		{
			$img->crop($param['w'], $param['h']);
		}
		$img->save($newPath);

		return $_uri;
	} catch (Exception $e) {
		return '<!-- can\'t create image - param src is incorrect or file doesn\'t exists  -->';
	}
	
	
}

